package com.isoft.src.main.java.service;

import com.isoft.src.main.java.model.User;
import com.isoft.src.main.java.util.UserFileHandler;

import java.util.ArrayList;
import java.util.List;

public class UserService {
    private List<User> users;

    public UserService() {
        this.users = UserFileHandler.loadUsers(); // Load users from file
    }

    public void registerUser(User user) {
        // Check if user already exists
        for (User u : users) {
            if (u.getUsername().equals(user.getUsername())) {
                System.out.println("用户名已存在.");
                return;
            }
        }
        // Add user and save to file
        users.add(user);
        UserFileHandler.saveUsers(users);
        System.out.println("用户注册成功.");
    }

    public boolean authenticate(String username, String password) {
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }
}
